<?php
/**
 * @title 2276. 统计区间中的整数数目
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class CountIntervals {
    /**
     */
    function __construct() {
        /**
         * @since 2023-12-16 区间的元素数量
         */
        $this->numCount = 0;

        /**
         * @since 2023-12-16 左右区间
         */
        $this->leftArray = [];
        $this->rightArray = [];
    }

    /**
     * @param Integer $left
     * @param Integer $right
     * @return NULL
     */
    function add($left, $right) {
        /**
         * @since 2023-12-16 为空
         */
        if($this->numCount == 0){
            $this->leftArray[] = $left;
            $this->rightArray[] = $right;
            $this->numCount = $right-$left+1;
        } else {}

        /**
         * @since 2023-12-16 二分插入
         */
        $leftPostion = $this->FindPosition($left, $this->leftArray);
        $rightPosition = $this->FindPosition($right, $this->rightArray);

        /**
         * @since 2023-12-16 区间数量
         */
        $rangeCount = count($this->leftArray);

        /**
         *
         * leftPosition = -2, left>maxLeft
         *   4,5,6
         *          9,10
         *
         * leftPosition = -1, left<=minLeft
         * 3,4,5
         *   4,5,6,7
         *   4,5,6     9,10
         *      6,7,8,9
         */
        /**
         * @since 2023-12-16 计算新增元素数量，合并区间
         * @since 2023-12-16 left>max(LeftArray)
         */
        if($leftPostion == -2){
            /**
             * @since 2023-12-16 最大right值
             * [1,2,3]
             *      3,4,5
             *        4,5
             */
            $maxRight = $this->rightArray[$rangeCount-1];
            if($left <= $maxRight+1) {
                /**
                 * @since 2023-12-16 合并最后一个区间，增加右边界
                 */
                $this->rightArray[$rangeCount-1] = $right;

                /**
                 * @since 2023-12-16 增加元素数量
                 */
                $this->numCount += $right-$maxRight;
            } else {
                /**
                 * @since 2023-12-16 新增一个区间
                 * [1,2,3]
                 *         5,6
                 */
                $this->leftArray[] = $left;
                $this->rightArray[] = $right;

                /**
                 * @since 2023-12-16 增加元素数量
                 */
                $this->numCount += $right-$left+1;
            }
        } elseif($leftPostion == -1) {
            /**
             * @since 2023-12-16 left<=min(leftArray) & right<=min(rightArray)
             */
            if($rightPosition == -1){
                if($right < $this->leftArray[0]-1){
                    /**
                     * @since 2023-12-16 新增一个区间
                     * 1,2
                     *      [4,5]
                     */
                    array_unshift($this->leftArray, $left);
                    array_unshift($this->rightArray, $right);

                    /**
                     * @since 2023-12-16 新增元素数量
                     */
                    $this->numCount += $right-$left+1;
                } else {
                    /**
                     * @since 2023-12-16 和第一个区间合并，right<=min(rightArray)
                     * 1,2,3
                     * 1,2,3,4
                     * 1,2,3,4,5
                     *      [4,5]
                     */
                    $minLeft = $this->leftArray[0];
                    $this->leftArray[0] = $left;

                    /**
                     * @since 2023-12-16 新增元素数量
                     */
                    $this->numCount += $minLeft-$left;
                }
            } elseif($rightPosition == -2) {
                /**
                 * @since 2023-12-16 right > max(rightArray)，所有区间合并成一个区间
                 */
                $this->leftArray = [$left];
                $this->rightArray = [$right];

                /**
                 * @since 2023-12-16 更新元素数量
                 */
                $this->numCount = $right-$left+1;
            } else {
                /**
                 * @since 2023-12-16 right <= rightArray[rightPosition+1]
                 * 1
                 * 1,2
                 * 1,2,3
                 *    [3,4,5]
                 */
                if($right >= $leftPostion[$rightPosition+1]-1){
                    /**
                     * @since 2023-12-16 和0合并到rightPosition+1
                     */
                    $mergeRange = $rightPosition+1;
                } else {
                    /**
                     * @since 2023-12-16 从0和并到rightPosition
                     */
                    $mergeRange = $rightPosition;
                }
                $mergeRight = max($right, $this->rightArray[$mergeRange]);

                /**
                 * @since 2023-12-16 累计合并区间内的元素数量
                 */
                $numCount = 0;
                for($i=0; $i<=$mergeRange; $i++){
                    $numCount += $this->rightArray[$i] - $this->leftArray[$i] + 1;

                    /**
                     * @since 2023-12-16 删该区间
                     */
                    unset($this->leftArray[$i]);
                    unset($this->rightArray[$i]);
                }

                /**
                 * @since 2023-12-16 新增区间
                 */
                array_unshift($this->leftArray, $left);
                array_unshift($this->rightArray, $mergeRight);

                /**
                 * @since 2023-12-16 统计新增元素数量
                 */
                $this->numCount += $mergeRight-$left+1 - $numCount;
            }
        } else {
            /**
             * @since 2023-12-16 leftArray[leftPosition] < left <= leftArray[leftPosition+1]
             * right > max(rightArray)
             */
            if($rightPosition == -2){
                /**
                 *      4,5,6
                 *        5,6,7
                 *          6,7,8
                 * [2,3,4]
                 */
                if($left <= $leftPostion[$leftPostion]+1){
                    /**
                     * @since 2023-12-16 开始合并的区间的起点
                     */
                    $mergePosition = $leftPostion;
                } else {
                    $mergePosition = $leftPostion+1;
                }
                $mergeLeft = min($left, $this->leftArray[$mergePosition]);

                /**
                 * @since 2023-12-16 合并区间
                 */
                $numCount = 0;
                for($i=$mergePosition; $i<$rangeCount; $i++){
                    $numCount += $this->rightArray[$i] - $this->leftArray[$i] + 1;

                    /**
                     * @since 2023-12-16 移除区间
                     */
                    unset($this->leftArray[$i]);
                    unset($this->rightArray[$i]);
                }

                /**
                 * @since 2023-12-16 新增元素数量
                 */
                $this->numCount += $right-$mergeLeft+1 - $numCount;
            } else {
                /**
                 * @since 2023-12-16 leftArray[leftPosition] < left <= leftArray[leftPosition+1]
                 * @since 2023-12-16 rightArray[rightPosition] < right <= rightArray[rightPosition+1]
                 */

                /**
                 * @since 2023-12-16 确定合并区间的范围，左边
                 *
                 *    2,3
                 * [1,2,3]  [5,6,7]
                 *      3,4,5
                 *        4,5,6
                 *          5,6,7
                 */
                if($left <= $leftPostion[$leftPostion]+1){
                    /**
                     * @since 2023-12-16 开始合并的区间的起点
                     */
                    $mergePositionS = $leftPostion;
                } else {
                    $mergePositionS = $leftPostion+1;
                }


            }
        }

    }

    /**
     * @return Integer
     */
    function count() {
        /**
         * @return
         */
        return $this->numCount;
    }

    /**
     * @title
     * @author start2004
     * @since 2023-12-16 Created
     *
     * @param int $num
     * @param array $numArray
     * @return int
     */
    function FindPosition($num = 0, $numArray = []){
        /**
         * @since 2023-12-16 元素数量
         */
        $numCount = count($numArray);

        /**
         * @since 2023-12-16 起点，终点
         */
        $s = 0;
        $e = $numCount-1;

        if($num <= $numArray[$s]){
            return -1;
        } elseif($num > $numArray[$e]){
            return -2;
        } else {
            /**
             * @since 2023-12-16 $s< $target <= $e
             */
            while(($e-$s)>1){
                $m = ($s+$e)>>1;
                $v = $numArray[$m];

                if($v>=$num){
                    $e = $m;
                } else {
                    $s = $m;
                }
            }

            /**
             * @return
             */
            return $s;
        }
    }
}

/**
 * Your CountIntervals object will be instantiated and called as such:
 * $obj = CountIntervals();
 * $obj->add($left, $right);
 * $ret_2 = $obj->count();
 */


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.16.php
 */
$datas = [

];

$obj = new CountIntervals();
var_dump($obj->add(3,2));

// include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
// $xhprof = new Xhprof();
// foreach ($datas as $data){
//     if(count($data) <= 20){
//         var_dump($data);
//     } else {
//         var_dump(count($data));
//     }
//
//     $obj = new Solution();
//     $result = $obj->(...$data);
//     // $result = $obj->($xhprof->tree($data));
//     // $result = $obj->($xhprof->listNode($data));
//     // $result = $obj->find(...$data);
//     var_dump($result);
//     // if(count($result)<=20){
//     //     var_dump($result);
//     // } else {
//     //     var_dump(count($result));
//     // }
//     echo str_repeat(PHP_EOL, 3);
//     echo str_repeat("<br>", 3);
// }

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
// $xhprof->end();
